package com.newsReader.category.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.game.hall.model.WXGameUser;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.newsReader.article.model.Article;
import com.newsReader.article.model.User;
import com.newsReader.banner.model.Banner;
import com.newsReader.category.model.CateUser;
import com.newsReader.category.model.Category;

public class CategoryApi  extends Controller {
	
	/**
	 * 指定路径,route里的url+/getModel 获取用户Model
	 * @return 
	 */
	public void queryAPI(){
		String id = getPara("id");
		String pageNum = getPara("pageNum");
		String pagesize = getPara("page_size");
		String nickName = (String)getSession().getAttribute("nickName");
		List<User> users = User.dao.UserDetail(nickName);
	    String userId = String.valueOf( users.get(0).get("id"));
		
	    Map<String, Object> map = new HashMap<String,Object>();
		//List<Category> querylist = Category.dao.findCategoryList();
		List<Category> Mycatelist = Category.dao.Mycate(Integer.parseInt(userId));//我的栏目
		List<Banner> bannerlist = Banner.dao.findBannerList();
		//List<Article> Articlelist = Article.dao.ArticleList(Integer.valueOf(id));
		String sql = "select cag.id caid,cag.title cagTitle,cag.imgurl caImg,arc.articleType,arc.title title, arc.imgUrl,timgurl,thimgurl,arc.puTime,arc.id,arc.sumcomen,arc.sumzan ";
		String resule = "FROM c_category cag INNER JOIN n_article arc ON cag.id=arc.coumnId where cag.id=?";
		Page<Article> Articlelist = Article.dao.paginate(Integer.parseInt(pageNum), Integer.parseInt(pagesize),sql,resule,id);
		map.put("querylist", Mycatelist);//栏目
	    map.put("bannerlist", bannerlist);//轮播图
	    map.put("Articlelist", Articlelist);//文章
		//user();
	    renderJson(map);
	}
	
	//我的栏目
	public void MycateAPI(){
		String nickName = (String)getSession().getAttribute("nickName");
		List<User> users = User.dao.UserDetail(nickName);
	    String userId = String.valueOf( users.get(0).get("id"));
		//String id = getPara("id");
		List<Category> querylist = Category.dao.findCategoryList();//查询栏目表
		List<Category> Mycatelist = Category.dao.Mycate(Integer.parseInt(userId));
		List<CateUser> cateUserlist = CateUser.dao.cateUser(Integer.parseInt(userId));
	
		String linCatid="";
		if(cateUserlist.size()>0){
			for(int j=0;j<cateUserlist.size();j++){
				String cateUId =String.valueOf(cateUserlist.get(j).get("cateid")); 
				CateUser cate = getModel(CateUser.class);
				String cateId="";
				for(int i=0;i<querylist.size();i++){
					cateId =String.valueOf(querylist.get(i).get("id"));
					if(cateUId.equals(cateId)){
						linCatid=cateId+",";
					}
					//CateUser.dao.insertCateUser(cate,Integer.valueOf(cateId),userId);
				}
				if("".equals(linCatid)){
					cate.set("cateId", cateId);
					cate.set("userId", userId);
					cate.save();
				}
			}
		}else{
			for(int i=0;i<querylist.size();i++){
				String cateId =String.valueOf(querylist.get(i).get("id"));
				CateUser cate = getModel(CateUser.class);
				cate.set("cateId", cateId);
				cate.set("userId", userId);
				cate.save();
			}
		}
		renderJson(Mycatelist);
		
	}
	//添加我的栏目
	public void  addMycateAPI(){
		String nickName = (String)getSession().getAttribute("nickName");
		List<User> users = User.dao.UserDetail(nickName);
	    String userId = String.valueOf( users.get(0).get("id"));
		String cateId = getPara("cateId");
		Category.dao.addMycate(Integer.parseInt(userId),Integer.valueOf(cateId));
		List<Category> Mycatelist = Category.dao.Mycate(Integer.parseInt(userId));
		renderJson(Mycatelist);
	}
	//删除我的栏目
	public void  delMycateAPI(){
		String nickName = (String)getSession().getAttribute("nickName");
		List<User> users = User.dao.UserDetail(nickName);
	    String userId = String.valueOf( users.get(0).get("id"));
		String cateId = getPara("cateId");
		Category.dao.delMycate(Integer.parseInt(userId),Integer.valueOf(cateId));
		List<Category> Mycatelist = Category.dao.Mycate(Integer.parseInt(userId));
		renderJson(Mycatelist);
	}
	
	public int user(){
		String openId = (String)getSession().getAttribute("openId");
		WXGameUser wxUser = (WXGameUser)getSession().getAttribute("wxUser");
		
		List<User> users = User.dao.UserDetail(openId);
		if(users.size()>0){
			//更新
			/*User user = getModel(User.class);
			user.set("headImgUrl",wxUser.getAvatarUrl() );
			user.set("nickName", wxUser.getNickName());
			user.set("sex", wxUser.getGender());
			user.set("createTime",new Date());
			user.update();*/
		}else{
			//添加
			User user = getModel(User.class);
			user.set("openId",openId);
			user.set("headImgUrl",wxUser.getAvatarUrl() );
			user.set("nickName", wxUser.getNickName());
			user.set("sex", wxUser.getGender());
			user.set("createTime",new Date());
			user.save();
		}
		return 1;
	}

}